//
//  BIZConsultRecordsVC.m
//  biz
//
//  Created by LXL on 2017/7/7.
//  Copyright © 2017年 jieshuibao. All rights reserved.
//

#import "BIZConsultRecordsVC.h"
#import "BIZOfficalVIPConsultDetailVC.h"

//cell
#import "BIZConsultRecordsCell.h"
//model
#import "BIZConsultRecordModel.h"

@interface BIZConsultRecordsVC ()
@property (nonatomic, assign) NSInteger lastId;
@property (nonatomic, strong) NSMutableArray *consultRecordArr;
@end

@implementation BIZConsultRecordsVC
- (BOOL)shouldAutorotate {
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"咨询记录";
    
    [self configTableView];

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
     self.lastId = 1;
     [self.consultRecordArr removeAllObjects];
     [self loadConsultRecordListData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.consultRecordArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BIZConsultRecordsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BIZConsultRecordsCell" forIndexPath:indexPath];
    cell.consultRecordModel = self.consultRecordArr[indexPath.section];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    BIZConsultRecordModel *model = self.consultRecordArr[indexPath.section];
    BIZOfficalVIPConsultDetailVC *detailVC = [[BIZOfficalVIPConsultDetailVC alloc] initWithUUID:model.uuid];
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (void)configTableView {
    self.tableView.estimatedRowHeight = 300;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.contentInset = UIEdgeInsetsMake(-36, 0, 0, 0);
    self.tableView.sectionHeaderHeight = 0;
    self.tableView.sectionFooterHeight = 10;
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.lastId = 1;
        [self.consultRecordArr removeAllObjects];
        [self loadConsultRecordListData];
    }];

    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter  footerWithRefreshingBlock:^{
        [self loadConsultRecordListData];
    }];

}

- (void)loadConsultRecordListData {
    NSMutableDictionary *paramDict = [NSMutableDictionary dictionary];
    paramDict[@"pageIndex"] = @(self.lastId);
    paramDict[@"pageEnd"] = @(self.lastId+9);
    
    [MBProgressHUD showAnimationHUD];
    [RequestTool POST:kOfficialRapidConsultRecordsListURL parameters:paramDict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        DeLog(@"咨询记录》》》》》》》：%@", responseObject);
        [MBProgressHUD hideHUD];
        [self stopRefresh];
        BIZConsultRecordArrModel *model = [BIZConsultRecordArrModel yy_modelWithJSON:responseObject];
        if (model.code == 1) {
            if (model.data.count) {
                self.lastId += model.data.count;
                [self.consultRecordArr addObjectsFromArray:model.data];
                [self.tableView reloadData];
                
            } else {
                self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
            }
            
        } else if (model.code == 2) {
            [MBProgressHUD showWarnMessage:model.des];
            BIZLoginViewController *vc = [BIZLoginViewController viewControllerFromStoryboard];
            MPWeakSelf(self);
            vc.loginSuccessBlock = ^(){
                [weakself loadConsultRecordListData];
            };
            [self presentViewController:vc animated:YES completion:nil];
        } else {
            [MBProgressHUD showWarnMessage:model.des];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        DeLog(@"咨询失败记录》》》》》》》：%@", error);
         [self stopRefresh];
        if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) {
            [MBProgressHUD showErrorMessage:@"网络不给力"];
        }
        else {
            [MBProgressHUD showErrorMessage:@"服务器繁忙"];
        }
    }];
    
}

- (void)stopRefresh {
    !self.tableView.mj_header.isRefreshing ? : self.tableView.mj_header.endRefreshing;
    !self.tableView.mj_footer.isRefreshing ? : self.tableView.mj_footer.endRefreshing;
}

- (NSMutableArray *)consultRecordArr {
    if (!_consultRecordArr) {
        _consultRecordArr = [NSMutableArray array];
    }
    
    return _consultRecordArr;
}


- (NSInteger)lastId {
    if (!_lastId) {
        _lastId = 1;
    }
    
    return _lastId;
}


@end
